﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class User_UserFile_Default : System.Web.UI.Page
{
    private string filePath = "";
    private string saveType = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["st"] != null)
        {
            saveType = Request["st"];
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        this.saveFileUpload();
        //this.emailSendDateTime.Value = curDateTime.ToString();
    }

    //保存上传文件信息如数据库
    protected void saveFileUpload()
    {
        if (this.fileUpload.HasFile)
        {
            string fileName = this.fileUpload.FileName;
            if (Session["user"] != null)
            {
                User curLoginUser = (User)Session["user"];
                string[] emailTargets = this.emailTarget.Value.Trim().Split(',');
                string[] emailTargetsUsername = this.emailTargetUsername.Value.Trim().Split(',');
                string filePrefix = "";
                if ("inform".Equals(saveType))
                {
                    filePrefix = FindConfigurationPath.instance().InformFilePath;
                    //如果目录不存在，则创建目录
                    if (!Directory.Exists(filePrefix))
                       Directory.CreateDirectory(filePrefix);

                    for (int i = 0; i < emailTargets.Length; i++)
                    {
                        filePath = filePrefix + curLoginUser.LoginName + "2" + emailTargets[i] + "_" + fileName;
                        try
                        {
                            this.fileUpload.SaveAs(filePath);
                            ScriptManager.RegisterStartupScript(Page, GetType(), "", "alert('文件上传成功!');", true);
                            this.ltbUploadFiles.Items.Add(new ListItem("文件名:" + fileName + "接收人:" + emailTargetsUsername[i], this.filePath.Replace(filePrefix, "")));
                        }
                        catch
                        {
                            ScriptManager.RegisterStartupScript(Page, GetType(), "", "alert('文件上传失败,请检查网络!');", true);
                        }
                    }
                }
                else if ("diary".Equals(saveType))
                {
                    filePrefix = FindConfigurationPath.instance().DiaryFilePath;
                    //如果目录不存在则创建目录
                    if (!Directory.Exists(filePrefix))
                        Directory.CreateDirectory(filePrefix);


                    filePath = filePrefix + curLoginUser.LoginName + "_" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
                        + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()
                        + DateTime.Now.Second.ToString() + "_" + fileName;
                    
                    try
                    {
                        this.fileUpload.SaveAs(filePath);
                        ScriptManager.RegisterStartupScript(Page, GetType(), "", "alert('文件上传成功!');", true);
                        this.ltbUploadFiles.Items.Add(new ListItem("文件名:" + fileName, filePath.Replace(FindConfigurationPath.instance().DiaryFilePath, "")));
                    }
                    catch (Exception ex)
                    {
                        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('" + ex.Message + "');", true);
                        ScriptManager.RegisterStartupScript(Page, GetType(), "", "alert('文件上传失败,请检查网络!');", true);
                    }
                }
                
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(Page, GetType(), "", "alert('文件不存在,请检查路径是否正确!');", true);
        }
    }
    protected void btnDelFile_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.ltbUploadFiles.Items.Count; i++)
        {
            if (ltbUploadFiles.Items[i].Selected)
            {
                string filePath = this.ltbUploadFiles.Items[i].Value;
                if(File.Exists(FindConfigurationPath.instance().InformFilePath + "\\" + filePath))
                {
                    File.Delete(filePath);
                    this.ltbUploadFiles.Items.Remove(this.ltbUploadFiles.SelectedItem);
                }
                i--;
            }
        }
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('文件已成功删除!');", true);
    }
}
